In [31]:
import os
import numpy as np
import cv2
import imutils
import argparse
import dlib

from skimage import io
from imutils import paths
In [32]:
from fdlite import FaceDetection, FaceLandmark, face_detection_to_roi
from fdlite import IrisLandmark, iris_roi_from_face_landmarks
from fdlite.examples.iris_recoloring import recolor_iris
from fdlite.transform import bbox_from_landmarks
from PIL import Image

from matplotlib import pyplot as plt

import re
import pandas as pd
In [33]:
detect_faces = FaceDetection()
detect_face_landmarks = FaceLandmark()
detect_iris = IrisLandmark()
In [40]:
test_files = []
for image_path in sorted(paths.list_images("pupil_images_v2+/")):
    if ".ipynb" not in image_path and "pupil" in image_path and "v3" not in image_path:
        test_files.append(image_path)
In [41]:
test_files
Out[41]:
['pupil_images_v2+/v2_pupil_detection_1.png',
 'pupil_images_v2+/v2_pupil_detection_10.png',
 'pupil_images_v2+/v2_pupil_detection_2.png',
 'pupil_images_v2+/v2_pupil_detection_3.png',
 'pupil_images_v2+/v2_pupil_detection_4.png',
 'pupil_images_v2+/v2_pupil_detection_5.png',
 'pupil_images_v2+/v2_pupil_detection_6.png',
 'pupil_images_v2+/v2_pupil_detection_7.png',
 'pupil_images_v2+/v2_pupil_detection_8.png',
 'pupil_images_v2+/v2_pupil_detection_9.png']
In [42]:
# Capture an image containing a face or faces
for image_path in test_files:
    img = Image.open(image_path)
    # face detections
    face_detections = detect_faces(img)
    # face roi
    face_roi = face_detection_to_roi(face_detections[0], img.size)
    # detect face landmarks
    face_landmarks = detect_face_landmarks(img, face_roi)
    # get ROI for both eyes
    eye_roi = iris_roi_from_face_landmarks(face_landmarks, img.size)
    # left eye, right eye
    left_eye_roi, right_eye_roi = eye_roi

    left_eye_results = detect_iris(img, left_eye_roi)
    right_eye_results = detect_iris(img, right_eye_roi)

    left_pupil = left_eye_results.iris[0]
    right_pupil = right_eye_results.iris[0]

    right_x = int(right_pupil.x * img.size[0])
    right_y = int(right_pupil.y * img.size[1])

    cv2_image = cv2.imread(image_path)
    cv2_image = cv2.circle(cv2_image, (right_x, right_y), radius=5, color=(0, 0, 255), thickness=-1)
    cv2_image = cv2.circle(cv2_image, (left_x, left_y), radius=5, color=(0, 0, 255), thickness=-1)
    rgb = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB)
    plt.imshow(rgb, cmap = plt.cm.Spectral)
    plt.imshow(rgb)
    plt.show()
In [43]:
left_pupil = left_eye_results.iris[0]
left_pupil
Out[43]:
Landmark(x=0.40227726, y=0.4388594, z=-0.005710603)
In [44]:
left_x = int(left_pupil.x * img.size[0])
left_x
Out[44]:
514
In [45]:
left_y = int(left_pupil.y * img.size[1])
left_y
Out[45]:
315
In [46]:
right_pupil = right_eye_results.iris[0]
right_pupil
Out[46]:
Landmark(x=0.6096785, y=0.42419234, z=-0.0022297213)
In [19]:
right_x = int(right_pupil.x * img.size[0])
right_x
Out[19]:
813
In [20]:
right_y = int(right_pupil.y * img.size[1])
right_y
Out[20]:
306
In [21]:
cv2_image = cv2.imread(image_path)
cv2_image = cv2.circle(cv2_image, (right_x, right_y), radius=5, color=(0, 0, 255), thickness=-1)
cv2_image = cv2.circle(cv2_image, (left_x, left_y), radius=5, color=(0, 0, 255), thickness=-1)
rgb = cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB)
plt.imshow(rgb, cmap = plt.cm.Spectral)
plt.imshow(rgb)
plt.show()
In [23]:
left_eye_roi
Out[23]:
Rect(x_center=0.43175293505191803, y_center=0.44361966848373413, width=0.21327190697193144, height=0.3791500568389892, rotation=0.15228863202777643, normalized=True)
In [ ]: